//
//  NRWBrandSeriseView.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/23.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWBrandTableView.h"
#import "NRWBrandSeriesCell.h"
#import "NRWBrandModel.h"

@interface NRWBrandTableView()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;

@end

@implementation NRWBrandTableView

- (void)setModel:(NRWBrandModel *)model {
    
    _model = model;
    
    [self.tableView reloadData];
}

- (void)scrollToSection:(NSInteger)section {
    
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:NO];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.tableView = [[UITableView alloc] init];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView registerClass:[NRWBrandSeriesCell class] forCellReuseIdentifier:@"cell"];
        [self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"header"];
        self.tableView.tableFooterView = DPView();
        [self addSubview:self.tableView];
    }
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];

    self.tableView.frame = self.bounds;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    //添加一行 不限车型
    return self.model.result.count + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //返回 不限车型
    if (section == 0) {
        return 1;
    }
    return self.model.result[section - 1].brandInfo.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NRWBrandSeriesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.section == 0 &&
        indexPath.row == 0) {
        cell.titleString = @"不限品牌";
        cell.logoString = @"brand_all";
        return cell;
    }
    
    NRWBrandInfoModel *model = self.model.result[indexPath.section - 1].brandInfo[indexPath.row];
    cell.titleString = model.brandName;
    cell.logoString = model.brandLogo;
    cell.didSelected = [self.selectBrandId isEqualToString:model.brandId];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate_brand respondsToSelector:@selector(brandTableView:didSelectedAtIndexPath:)]) {
        
        [self.delegate_brand brandTableView:self didSelectedAtIndexPath:indexPath];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    if ([self.delegate_brand respondsToSelector:@selector(brandTableViewWillBeginDragging:)]) {
        
        [self.delegate_brand brandTableViewWillBeginDragging:self];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 45;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *header = DPView().setBackgroundColor(backColor()).setFrame(CGRectMake(0, 0, SCREEN_WIDTH(), 20));
    
    UILabel *label = DPLabel().setTextColor(getColor(@"#333333")).setFont(getThemeFont(12)).setFrame(CGRectMake(17, 0, SCREEN_WIDTH() - 17, 20));
    if (section == 0) {
        label.text = @"不限车型";
    } else {
        label.text = self.model.indexBarInitialArr[section - 1];
    }
    header.addSubview(label);
    
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 20.f;
}

@end
